/**
 * 自定义数组扩容规则，当已存储元素数量达到总容量的 80%时，扩容 1.5 倍。
 */
import java.util.Scanner;
public class ArrayResizeTest {

    public static void main(String[] args){
        //声明扫描器，获取初始化容量
        System.out.println("请输入数组初始化容量");
        Scanner scanner = new Scanner(System.in);
        int arraySize = scanner.nextInt();
        arraySize = Math.max(arraySize, 10);
        //对数据进行初始化
        int[] array1 = new int[arraySize];
        int[] array2 = new int[arraySize];
        int i = 1;
        int threadHold = (int)(arraySize * 0.8);
        boolean flag = true;
        while(i<=arraySize){
            System.out.println("请输入数组第" + i + "个元素");
            Scanner scanner1 = new Scanner(System.in);
            int numOfArray = scanner.nextInt();
            int arrInd = i - 1;
            if(flag){
                array1[arrInd] = numOfArray;
            }else{
                array2[arrInd] = numOfArray;
            }
            if(i>=threadHold){
                System.out.println("数组开始进行扩容");
                int nArraySize = arraySize + (arraySize >> 1);
                System.out.println("数组扩容大小为" + nArraySize);
                if(flag){
                    array2 = new int[nArraySize];
                    for(int j=0;j<=threadHold;j++){
                        array2[j] = array1[j];
                    }
                }else{
                    array1 = new int[nArraySize];
                    for(int j=0;j<=threadHold;j++){
                        array1[j] = array2[j];
                    }
                }
                arraySize = nArraySize;
                threadHold = (int)(arraySize * 0.8);
                flag = !flag;
            }
            i++;
        }

    }
    
}